//
//  WZSuggestView.m
//  ETong
//
//  Created by wanzhao on 15/11/1.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZSuggestView.h"
#import "MBProgressHUD+WZ.h"

@interface WZSuggestView ()<UITextViewDelegate>

@property (nonatomic, weak) UILabel *placeHolderLabel;
@property (nonatomic, weak) UIImageView *backView;
@property (nonatomic, weak) UITextView *textView;

@end

@implementation WZSuggestView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *backView = [[UIImageView alloc] init];
        backView.backgroundColor = ClearColor;
        backView.userInteractionEnabled = YES;
        [self addSubview:backView];
        self.backView = backView;
        
        
        UITextView *textView = [[UITextView alloc] init];
        textView.backgroundColor = ClearColor;
        textView.delegate = self;
        textView.returnKeyType = UIReturnKeyDone;
        textView.autocorrectionType = UITextAutocorrectionTypeNo;
        [self addSubview:textView];
        self.textView = textView;
        
        UILabel *placeLabel = [[UILabel alloc] init];
        placeLabel.backgroundColor = ClearColor;
        placeLabel.font = self.textView.font;
        placeLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:placeLabel];
        self.placeHolderLabel = placeLabel;
    }
    return self;
}

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    _backgroundImage = backgroundImage;
    self.backView.image = backgroundImage;
    self.backView.userInteractionEnabled = YES;
}

- (void)setText:(NSString *)text
{
    self.textView.text = text;
}

- (NSString *)text
{
    return self.textView.text;
}
- (void)setPlaceFont:(UIFont *)placeFont
{
    _placeFont = placeFont;
    self.placeHolderLabel.font = placeFont;
}

- (void)setPlaceColor:(UIColor *)placeColor
{
    _placeColor = placeColor;
    self.placeHolderLabel.textColor = placeColor;
}
- (void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder = [placeHolder copy];
    if (self.text.length > 0) {
        self.placeHolderLabel.hidden = YES;
        self.placeHolderLabel.text = placeHolder;
    } else {
        self.placeHolderLabel.hidden = NO;
        self.placeHolderLabel.text = placeHolder;
    }
    
    [self setLabelFont];
}

- (void)setFont:(UIFont *)font
{
    _font = font;
    self.textView.font = font;
    self.placeHolderLabel.font = font;
    [self setLabelFont];
}

- (void)setLabelFont
{
    // 2.计算label的frame
    CGSize labelMaxSize = CGSizeMake(HARDWARE_SCREEN_WIDTH - OppositeWidthRate(10), MAXFLOAT);
    CGSize labelSize = [self.placeHolder sizeWithFont:self.placeHolderLabel.font maxSize:labelMaxSize];
    self.placeHolderLabel.size = labelSize;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString: @"\n"]) {
        // Hide the keyboard
        [textView resignFirstResponder];
        
        // Also return if its showing a placeholder
        return NO ;
    }
    if ([text isEqualToString:@""]) {
        return YES;
    }
    
    if (textView.text.length > 499) {
        [MBProgressHUD showError:@"文字个数已达到最大值"];
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    
    if (textView.text.length > 0) {
        self.placeHolderLabel.hidden = YES;
        if (textView.text.length > 500) {
            textView.text = [textView.text substringToIndex:499];
            self.text = textView.text;
        }
        
    } else {
        self.placeHolderLabel.hidden = NO;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.textView.frame = CGRectMake(0, 0, self.width, self.height);
    self.placeHolderLabel.x = OppositeWidthRate(5);
    self.placeHolderLabel.y = OppositeHeightRate(5);
    self.backView.frame = CGRectMake(0, 0, self.width, self.height);
}
@end
